/*
 * Copyright 2018 Youchaoshi authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package common.tool;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * SharedPreference工具
 */
public class MySharedPreference {

    public static void save(String name, String value, Context context) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(
                "text", Activity.MODE_PRIVATE);
        Editor editor = mySharedPreferences.edit();
        editor.putString(name, value);
        editor.commit();
    }

    public static void saveBitmapToSharedPreferences(String key, Bitmap bitmap,
                                                     Context context) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(
                "text", Activity.MODE_PRIVATE);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 80, byteArrayOutputStream);

        byte[] byteArray = byteArrayOutputStream.toByteArray();
        String imageString = new String(Base64.encodeToString(byteArray, Base64.DEFAULT));


        Editor editor = mySharedPreferences.edit();
        editor.putString(key, imageString);
        editor.commit();
    }


    public static Bitmap getBitmapFromSharedPreferences(String key, String def,
                                                        Context context) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(
                "text", Activity.MODE_PRIVATE);

        String imageString = mySharedPreferences.getString(key, def);

        byte[] byteArray = Base64.decode(imageString, Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                byteArray);

        Bitmap bitmap = BitmapFactory.decodeStream(byteArrayInputStream);
        return bitmap;
    }

    public static String get(String name, String defvalue, Context context) {
        if (context == null) {
            return "";
        }
        if (name == null) {
            return "";
        }
        if (defvalue == null) {
            return "";

        }
        SharedPreferences mySharedPreferences = context.getSharedPreferences(
                "text", Activity.MODE_PRIVATE);
        if (mySharedPreferences == null) {
            return "";
        } else
            return mySharedPreferences.getString(name, defvalue);
    }

    public static void clear(Context context) {
        SharedPreferences mySharedPreferences = context.getSharedPreferences(
                "text", Activity.MODE_PRIVATE);
        Editor editor = mySharedPreferences.edit();
        editor.remove("userId");
        editor.clear();
        editor.commit();
    }
}
